package classics;

import java.sql.SQLOutput;
import java.util.Arrays;
import java.util. List;
import java.util.Random;

public class Array {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        for (int num:arr){
            System.out.print(num+" ");
        }

        System.out.println();
        int sum =0;
        for (int num:arr){
            sum +=num;
        }
        System.out.println("数组总和是："+sum);

        int max=arr[0] ;
        int min =arr[0];
        for (int num:arr){
            if (num>max){
                max=num;
            }
            if (num<min){
                min=num;
            }
        }
        System.out.println("最大值为："+max+",最小值为："+min);


        int[] arr1 = new int[10];//可以先声明 后初始化
        int[] arr2 = new int[]{1,2,3,4,5};//可以先声明 后初始化
        int[] arr3 ={1,2,3,4,5,6};//必须声明并初始化

        arr[1] = 100;
        arr[2] = 57;

        for (int i = 0; i < arr.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j]>arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    flag = true;
                }
            }
            if (!flag){
                break;
            }
        }
        System.out.print("排序后的数组为：");
        for(int num:arr){
            System.out.print(num+" ");
        }
        System.out.println();

        int target = 5;
        boolean found=false;
        for (int num:arr){
            if (num==target){
                found=true;
                break;
            }
        }
        if (found){
            System.out.println("存在。");
        }else {
            System.out.println("不存在。");
        }

        int[] array = new int[arr.length];
        for (int i = 0;i<arr.length;i++) {
            array[i] = arr[i];
        }
        System.out.print("复制后的数组遍历：");
        for (int num:array){
            System.out.print(num+" ");
        }
        System.out.println();
        int[] array2 = new int[10];
        System.out.print("随机自然赋值数组：");
        for (int num : array2){
            num = (int)(Math.random()*1000);
            System.out.print(num+" ");
        }


        System.out.println();
        int[] array3 = new int[]{1,2,3,4,5};
        int[] array4= Arrays.copyOf(array3,7);//创建数组副本
        for (int num:array4){
            System.out.print(num+" ");
        }

        System.out.println();

        System.out.print("展示使用填充方法后的数组：");
        Arrays.fill(array2,5);
        for (int num:array2){
            System.out.print(num+" ");
        }

        System.out.println();

        System.out.println("二分查找方法：");
        System.out.println(Arrays.binarySearch(arr,11));

        int[] array5 = new int[5];
        System.out.print("array5:");
        for (int n= 0;n<array5.length;n++){//增强型for循环中，迭代的是数组中取出的元素副本
            array5[n]=(int)(Math.random()*1000);
            System.out.print(array5[n]+" ");
        }
        System.out.println();
        System.out.print("排序：");
        Arrays.sort(array5);
        System.out.println(Arrays.toString(array5));

        int[] array6 ={1,3,4,0,0,0};
        System.out.println("arraycopy演示结果array6：");
        System.arraycopy(array5,0,array6,2,2);
        for (int num:array6){
            System.out.print(num+" ");
        }
        System.out.println();
        System.out.print("array7:");
        int[] array7 = array6.clone();
        for (int num:array7){
            System.out.print(num+" ");
        }
        System.out.println();
        System.out.print("扩容后的array7：");//创建后的数组长度是固定不变的，所谓扩容不过是创建了一个数组副本
        array7=Arrays.copyOf(array7,9);
        for (int num:array7){
            System.out.print(num+" ");
        }
        System.out.println();
        System.out.println(Arrays.equals(array6,array7));

       List<Integer> list=Arrays.asList(1,2,3,4);

       int [] array8 ;

       array8=Arrays.copyOfRange(array7,0,7);
       for (int num:array8){
           System.out.print(num+" ");
       }

       //简化代码编写，提高代码可读性.arrays.aslist（）方法参数是一个可变参数，可以接受任何对象类型作为参数，很明显整数型数组中的元素并不是对象
        //所以将整数型数组放入参数会被视为一个对象元素，而string类型中的元素本身就是对象，故传入形参，编译器会将其视为一个对象序列。
        System.out.println();
        System.out.print("自然数对象的提供随机整数方法演示：");
        Random random = new Random();
       int[] array9 =new int[5];
        for (int i = 0; i < array9.length; i++) {
            array9[i] = random.nextInt(100);
            System.out.print(array9[i]+" ");
        }

    }
}
